<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterName">
              <j-popup
                v-model="model.semesterName"
                field="semesterName"
                org-fields="id,name"
                dest-fields="semester,semesterName"
                code="ca_semester"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeName">
              <j-popup
                v-model="model.gradeName"
                field="gradeName"
                org-fields="id,code,name"
                dest-fields="grade,gradeCode,gradeName"
                code="ca_grade"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教师" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherName">
              <j-popup
                v-model="model.teacherName"
                field="teacherName"
                org-fields="id,code,name"
                dest-fields="teacher,teacherCode,teacherName"
                code="ca_teacher"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courseName">
              <j-popup
                v-model="model.courseName"
                field="courseName"
                org-fields="id,code,name"
                dest-fields="course,courseCode,courseName"
                code="ca_course"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classsName">
              <j-popup
                v-model="model.classsName"
                field="classsName"
                org-fields="id,code,name"
                dest-fields="classs,classsCode,classsName"
                code="ca_existing_classs"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="共多少周" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weeks">
              <a-input-number v-model="model.weeks" placeholder="请输入共多少周" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="每周多少节课" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weeklyCourseNum">
              <a-input-number v-model="model.weeklyCourseNum" placeholder="请输入每周多少节课" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="共多少节课" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalCourseNum">
              <a-input-number v-model="model.totalCourseNum" placeholder="请输入共多少节课" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CaTeachingPlanForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           code: [
              { required: true, message: '请输入编码!'},
              { validator: (rule, value, callback) => validateDuplicateValue('ca_teaching_plan', 'code', value, this.model.id, callback)},
           ],
           semesterName: [
              { required: true, message: '请输入学期!'},
           ],
           gradeName: [
              { required: true, message: '请输入年级!'},
           ],
           teacherName: [
              { required: true, message: '请输入教师!'},
           ],
           courseName: [
              { required: true, message: '请输入课程!'},
           ],
           classsName: [
              { required: true, message: '请输入班级!'},
           ],
           weeks: [
              { required: true, message: '请输入共多少周!'},
           ],
           weeklyCourseNum: [
              { required: true, message: '请输入每周多少节课!'},
           ],
        },
        url: {
          add: "/ca/caTeachingPlan/add",
          edit: "/ca/caTeachingPlan/edit",
          queryById: "/ca/caTeachingPlan/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>